using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;


namespace RUPS
{
    public partial class A_NIR : Form
    {
        SqlDataAdapter adaptorMat;
        DataSet dataset = new DataSet();
        SqlDataAdapter adaptorCom;
        SqlConnection conexiune= new SqlConnection();

        public int idO;
        public int factO;
        public int idComO;
        public decimal pretO;
        public decimal tvaO;
        public int cantO;
        public DateTime dataReceptieO;
        public DateTime dataComandaO;




        public A_NIR()
        {
            InitializeComponent();
        }

        private void NIR_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nIRDS.NIR' table. You can move, or remove it, as needed.
            
            try
            { // TODO: This line of code loads data into the 'masivDBDataSet2.NIR' table. You can move, or remove it, as needed.
                this.nIRTableAdapter.Fill(this.nIRDS.NIR);


                 adaptorCom = new SqlDataAdapter("select id from comenziMateriale  where statusComanda = 'false' ", nIRTableAdapter.Connection);
                


                adaptorCom.Fill(dataset, "comenzi");
                this.a_comanda.DataSource = dataset.Tables["comenzi"];
                this.a_comanda.DisplayMember = dataset.Tables["comenzi"].Columns["id"].ToString();
                this.a_comanda.ValueMember = dataset.Tables["comenzi"].Columns["id"].ToString();

                conexiune.ConnectionString = nIRTableAdapter.Connection.ConnectionString;
                conexiune.Open();
                SqlDataReader reader;
                SqlCommand comanda;

                comanda = new SqlCommand("select cantitate, idMaterial, dataComanda from comenziMateriale where statusComanda = 'false' and id = "+ int.Parse(a_comanda.SelectedValue.ToString()), conexiune);
                reader = comanda.ExecuteReader();
                while (reader.Read())
                {
                     dataComandaO = DateTime.Parse(reader["dataComanda"].ToString());
                    this.a_dataComanda.Text = dataComandaO.ToShortDateString();


                    this.a_cant_doc.Text = reader["cantitate"].ToString();
                    this.a_codProdus.Text = reader["idMaterial"].ToString();
                }
                reader.Close();
                

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        #region validari
        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <='9' || e.KeyChar == 8 || e.KeyChar == ',')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }

        private void a_pret_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <='9' || e.KeyChar == 8 || e.KeyChar == ',')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }

        private void a_tva_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >='0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == ',')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }

        private void a_cant_doc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <='9' || e.KeyChar == 8 || e.KeyChar == ',')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }

        private void a_receptionata_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == ',')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }
        #endregion
        #region adauga
        private void adauga_Click(object sender, EventArgs e)
        {
            try
            {
                if (a_nrFactura.Text == "")
                {
                    a_eroare.SetError(a_nrFactura, "completati nr facturii!!!");
                    throw new InvalidOperationException("nu sunt completate datele obligatorii");
                }
                else
                    a_eroare.SetError(a_nrFactura, "");

                if (a_pret.Text == "")
                {
                    a_eroare.SetError(a_pret, "completati pretul!!!");
                    throw new InvalidOperationException("nu sunt completate datele obligatorii");
                }
                else
                    a_eroare.SetError(a_pret, "");
                if (a_tva.Text == "")
                {
                    a_eroare.SetError(a_tva, "completati tva!!!");
                    throw new InvalidOperationException("nu sunt completate datele obligatorii");
                }
                else
                    a_eroare.SetError(a_tva, "");

                if (a_receptionata.Text == "")
                {
                    a_eroare.SetError(a_receptionata, "completati cantitatea receptionata!!!");
                    throw new InvalidOperationException("nu sunt completate datele obligatorii");
                }
                else
                    a_eroare.SetError(a_receptionata, "");

                nIRTableAdapter.Insert(int.Parse(a_nrFactura.Text),
                    int.Parse(a_comanda.SelectedValue.ToString()), decimal.Parse(a_pret.Text),
                    decimal.Parse(a_tva.Text), int.Parse(a_receptionata.Text),
                    DateTime.Parse(a_dataReceptie.Value.ToString()));

                nIRDSTableAdapters.NIRTableAdapter dta = new RUPS.nIRDSTableAdapters.NIRTableAdapter();
                nIRDS.NIRDataTable tb = new nIRDS.NIRDataTable();
                dta.Fill(tb);

                gridNir.DataSource = null;
                nIRBindingSource.ResetBindings(false);
                nIRBindingSource.DataSource = tb;
                gridNir.DataSource = nIRBindingSource;
                gridNir.Invalidate();


                //insert si in tabela de materiale
                SqlDataReader reader;
                if(conexiune.State == ConnectionState.Closed)
                    conexiune.Open();
                SqlCommand select = new SqlCommand("select dimensiune from tipMaterial where id = " + int.Parse(a_codProdus.Text), conexiune);
                reader = select.ExecuteReader();
                int dimens = 0;
                while (reader.Read())
                {
                    dimens = int.Parse(reader["dimensiune"].ToString());
                }
                reader.Close();
                SqlCommand comanda = new SqlCommand("insert into materiale(idTip, dimensiune) values (" +
                    int.Parse(a_codProdus.Text) + "," + dimens + ")", conexiune);

                for (int i = 1; i <= int.Parse(a_receptionata.Text); i++)
                {
                    comanda.ExecuteNonQuery();
                }

                comanda = new SqlCommand("update comenziMateriale set statusComanda = 'true' where id = " + int.Parse(a_comanda.SelectedValue.ToString()), conexiune);
                comanda.ExecuteNonQuery();
              

/////////////////////////////reincarc comboboxul/////////////////////////////////////////////////////////////////
                a_comanda.DataSource = null;
                a_comanda.Items.Clear();
                a_comanda.Text = "";
                adaptorCom = new SqlDataAdapter("select id from comenziMateriale  where statusComanda = 'false' ", conexiune);
                adaptorCom.Fill(dataset, "comenzi");
 
                this.a_comanda.DataSource = dataset.Tables["comenzi"];
                this.a_comanda.DisplayMember = dataset.Tables["comenzi"].Columns["id"].ToString();
                this.a_comanda.ValueMember = dataset.Tables["comenzi"].Columns["id"].ToString();
                conexiune.Close();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }

        }
        #endregion
        private void revoca_Click(object sender, EventArgs e)
        {
            this.a_nrFactura.Text = "";
            this.a_dataReceptie.Value = DateTime.Today;
            this.a_comanda.Text = "";
            this.a_pret.Text = "";
            this.a_tva.Text = "";
            this.a_codProdus.Text = "";
            this.a_cant_doc.Text = "";
            this.a_receptionata.Text = "";
            this.a_dataComanda.Text = "";
            adauga.Enabled = true;

        }

        private void gridNir_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
               
                DataGridViewRow rand = gridNir.SelectedRows[0];
                a_nrFactura.Text = rand.Cells[1].Value.ToString();
                factO = int.Parse(a_nrFactura.Text.ToString());

                a_pret.Text = rand.Cells[3].Value.ToString();
                pretO = decimal.Parse(a_pret.Text.ToString());

                a_tva.Text = rand.Cells[4].Value.ToString();
                tvaO = decimal.Parse(a_tva.Text.ToString());

                a_receptionata.Text = rand.Cells[5].Value.ToString();
                cantO = int.Parse(a_receptionata.Text.ToString());

                idComO = int.Parse(rand.Cells[2].Value.ToString());

                int idx = a_comanda.FindStringExact(rand.Cells[2].Value.ToString());
                //a_comanda.SelectedIndex = idx;
                //a_comanda.SelectedText = idComO.ToString();
               // a_comanda.SelectedValue = idComO;
                a_comanda.Text = "";
                a_comanda.Text = idComO.ToString();

                idO = int.Parse(rand.Cells[0].Value.ToString());
               
                dataReceptieO = DateTime.Parse(rand.Cells[6].Value.ToString());

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                adauga.Enabled = false;

                if(conexiune.State == ConnectionState.Closed)
                    conexiune.Open();
                SqlCommand comanda = new SqlCommand("select cantitate, idMaterial, dataComanda from comenziMateriale  where  id = " + int.Parse(rand.Cells[2].Value.ToString()), conexiune);
                SqlDataReader reader = comanda.ExecuteReader();
                while (reader.Read())
                {
                  dataComandaO = DateTime.Parse(reader["dataComanda"].ToString());
                    this.a_dataComanda.Text = dataComandaO.ToShortDateString();
                    this.a_cant_doc.Text = reader["cantitate"].ToString();
                    this.a_codProdus.Text = reader["idMaterial"].ToString();
                }
                
                reader.Close();
                conexiune.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                conexiune.Close();
            }



        }
        #region
        //private void sterge_Click(object sender, EventArgs e)
        //{
        //     if (MessageBox.Show("Stergeti Nir?", "Confirmare", MessageBoxButtons.OKCancel) == DialogResult.OK)
        //    {  
        //       try
        //       {
        //           conexiune = nIRTableAdapter.Connection;
        //           conexiune.Open();
        //           SqlCommand comanda = new SqlCommand("delete from nir where id = " + idO, conexiune);
        //           comanda.ExecuteNonQuery();

        //           nIRDSTableAdapters.NIRTableAdapter tac = new RUPS.nIRDSTableAdapters.NIRTableAdapter();

        //           nIRBindingSource.ResetBindings(false);
        //           gridNir.DataSource = null;

        //           nIRDS.NIRDataTable dt = new nIRDS.NIRDataTable();
        //           tac.Fill(dt);
        //           nIRBindingSource.DataSource = dt;
        //           gridNir.DataSource = nIRBindingSource;
        //           gridNir.Invalidate();
        //           conexiune.Close();

        //       }
        //       catch(Exception ex)
        //       {
        //           MessageBox.Show(ex.Message);
        //           conexiune.Close();
        //       }
        //   }

           
        //}

        //private void modifica_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        #region date vechi
        //        //DateTime data = a_dataReceptie.Value;

        //        //nIRTableAdapter.UpdateNIR(int.Parse(a_nrFactura.Text), int.Parse(a_comanda.SelectedValue.ToString()),
        //        //    decimal.Parse(a_pret.Text), decimal.Parse(a_tva.Text), int.Parse(a_receptionata.Text),
        //        //    data, idO, factO, idComO, pretO, tvaO, cantO, dataReceptieO);


        //        //gridNir.DataSource = null;
        //        //nIRBindingSource.ResetBindings(false);
        //        //nIRDSTableAdapters.NIRTableAdapter dta = new RUPS.nIRDSTableAdapters.NIRTableAdapter();
        //        //nIRDS.NIRDataTable tb = new nIRDS.NIRDataTable();
        //        //dta.Fill(tb);

        //        //nIRBindingSource.DataSource = tb;
        //        //gridNir.DataSource = nIRBindingSource;
        //        //gridNir.Invalidate();
        //        //conexiune.Close();
        //        #endregion date noi


        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }

        //}
        #endregion

        private void a_comanda_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if(conexiune.State == ConnectionState.Closed)
                conexiune.Open();
           SqlCommand comanda = new SqlCommand("select cantitate, idMaterial, dataComanda from comenziMateriale  where statusComanda = 'false' and id = "+ int.Parse(a_comanda.SelectedValue.ToString()), conexiune);
           SqlDataReader    reader = comanda.ExecuteReader();
                while (reader.Read())
                {
                    dataComandaO = DateTime.Parse(reader["dataComanda"].ToString());
                    this.a_dataComanda.Text = dataComandaO.ToShortDateString();

                    this.a_cant_doc.Text = reader["cantitate"].ToString();
                    this.a_codProdus.Text = reader["idMaterial"].ToString();
                }
                reader.Close();
                conexiune.Close();
        }

        }

        
    }
